<?php 		
class CForumController extends CController{

	protected $m_arrobjForums;
	protected $m_objForum;
	protected $m_objFCKeditor;
		
	function __construct() {
		parent::__construct();
		return true;
	}
	
	function create() {
		parent::create();		
	}
	
	function initialize() {
		parent::initialize();
	}
	
	function execute() {		
		
		switch( $this->m_strAction ) {
			
			case NULL:			
			case 'list':
				$this->handleViewForumList();
				break;
				
			case 'add':									
			case 'edit':
				$this->handleAddEditForum();
				break;
				
			case 'insert_update_forum':
				$this->handleInsertUpdateForum();
				break;
			
			case 'update_forum_action':
				$this->handleUpdateForumAction();
				break;
			
			case 'view':
				$this->handleViewForum();
				break;
				
			default:
				// Empty default case
				
		}
	}

	/*************************************
	********** HANDLE FUNCTIONS **********
	*************************************/
		
	function handleViewForumList() {
		
		$intParentId = $this->m_objData->getGetData( array( 'parent_id' ) );

		if( true == isset( $intParentId ) && 0 < $intParentId ) {
			$this->m_arrobjForums = CForums::fetchForumsByParentId( $intParentId, $this->m_objDatabase );
			$this->m_objForum = $this->viewForumById( $intForumId );
		} else {
			$this->m_arrobjForums = CForums::fetchForums( $this->m_objDatabase );
		}
		
		$this->displayForumList();
	}
	
	function handleAddEditForum() {
		
		include( "Liabraries/fckeditor/fckeditor.php");
		
		$this->m_objFCKeditor = new FCKeditor('forum[description]') ;
		$this->m_objFCKeditor->BasePath = SITE_PATH . '/Liabraries/fckeditor/';		
		$this->m_objFCKeditor->Width  = '100%';
		$this->m_objFCKeditor->Height = '500';
		$intForumId = $this->m_objData->getRequestData( array( 'id' ) );
		$this->m_objForum = new CForum();
		if( true == isset( $intForumId ) ) {
			$this->m_objForum = $this->viewForumById( $intForumId );
			$this->m_objFCKeditor->Value  = $this->m_objForum->getDescription();
		}
		
		$this->displayAddEditForum();
	}

	function handleViewForum() {
		
		$intForumId = $this->m_objData->getRequestData( array( 'id' ) );		
		if( true == isset( $intForumId ) ) {
			$this->m_objForum = $this->viewForumById( $intForumId );
		}	
		$this->displayViewForum();
	}
	
	function handleInsertUpdateForum() {
		
		switch( true ) {
			default:
				$arrmixFieldsValues = $this->m_objData->getPostData( array( 'forum' ) );
		
				$this->m_objForum = CForums::setValues( 'CForum', $arrmixFieldsValues );
				
				if( false == $this->m_objForum->validate( $arrmixFieldsValues, array( 'title', 'description' ) ) ) {
					break;
				}
				
				if( 0 < $arrmixFieldsValues['id'] ) {
				
					if( false == $this->m_objForum->update( $this->m_objDatabase ) ) {
						$this->m_objErrorMessage->setErrorMessage( 'Failed to update forum.' );
					} else {
						$this->m_objErrorMessage->setSuccessMessage( 'Forum update successfully. ' );
					}
				} else {
				
					if( false == $this->m_objForum->insert( $this->m_objLoginUser->getId(), $this->m_objDatabase ) ) {
						$this->m_objErrorMessage->setErrorMessage( 'Failed to create Forum.' );
					} else {
						$this->m_objErrorMessage->setSuccessMessage( 'Forum created successfully. ' );
					}
				}
				redirect( '/admin/forum' );
		}
 		redirect( '/admin/forum/add' );
	}
	
	function handleUpdateForumAction() {
		
		switch( true ) {
			default:
				
				$strAction = $this->m_objData->getPostData( array( 'forum', 'action' ) );				
				if( false == isset( $strAction  ) ) {
					$this->m_objErrorMessage->setErrorMessage( 'Please select action ' );
					break;
				}
				
				$arrintForumIds = $this->m_objData->getPostData( array( 'forum', 'id' ) );
				if( false == valArr( $arrintForumIds ) ) {
					$this->m_objErrorMessage->setErrorMessage( 'Please select records to delete ' );
					break;
				}
								
				$this->m_objForum = new CForum();
				if( false == valObj( $this->m_objForum, 'CForum' ) ) {
					$this->m_objErrorMessage->setErrorMessage( 'Failed to create object' );
					break;
				}
				
				if( 'delete' == $strAction ) {
					if( true == $this->m_objForum->deleteForumsById( $arrintForumIds, $this->m_objDatabase ) ) {
						$this->m_objErrorMessage->setSuccessMessage( 'Record(s) deleted successfully.' );
					}
				} else {
					if( true == $this->m_objForum->updateForumsActionById( $strAction, $arrintForumIds, $this->m_objDatabase ) ) {
						$this->m_objErrorMessage->setSuccessMessage( 'Record(s) updated successfully.' );
					}
				}
									
		}
		redirect( '/admin/forum' );
	}
	
	/**************************************
	********** OTHER FUNCTIONS **********
	**************************************/
	
	function viewForumById( $intForumId ) {
		return CForums::fetchForumById( $intForumId, $this->m_objDatabase );		
	}
	
	/**************************************
	********** DISPLAY FUNCTIONS **********
	**************************************/		
	
	function displayForumList() {
		$this->display();

		$this->m_objSmarty->assign_by_ref( 'forums', $this->m_arrobjForums );
		$this->m_objSmarty->assign_by_ref( 'forum', $this->m_objForum );
		$this->m_objSmarty->display( TEMPLATE_DIR_PATH . '/admin/forum/view_forum_list.tpl');
	}
	
	function displayAddEditForum() {
		$this->display();
		
		$this->m_objSmarty->assign_by_ref( 'fckeditor', $this->m_objFCKeditor );
		$this->m_objSmarty->assign_by_ref( 'forum', $this->m_objForum );
		$this->m_objSmarty->display( TEMPLATE_DIR_PATH . '/admin/forum/view_add_edit_forum.tpl' );
	}
	
	function displayViewForum() {
		$this->display();

		$this->m_objSmarty->assign_by_ref( 'forum', $this->m_objForum );		
		$this->m_objSmarty->display( TEMPLATE_DIR_PATH . '/admin/forum/view_forum.tpl');

	}
	
	function display() {
		
		parent::display();
		$this->m_objSmarty->assign( 'leftadminmenu', 'forums' );
	}
}
?>